package com.soft.sitegenerator.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.soft.sitegenerator.Component;
import com.soft.sitegenerator.DataBaseComponent;
import com.soft.sitegenerator.business.hibernate.Sheet;
import com.soft.sitegenerator.business.hibernate.WebSite;
import com.soft.sitegenerator.business.hibernate.SitePage;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;
import com.soft.sitegenerator.form.SitePageEditForm;

public class SitePageEditAction extends DispatchAction {

	public ActionForward editSitePage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SitePageEditForm sitePageEditForm = (SitePageEditForm) form;

		Long id = new Long(Long.parseLong(request.getParameter("id")));

		SitePage sitePage = sitePageEditForm.getSitePage();
		sitePage.setId(id);

		try {
			sitePage.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		Component component = new DataBaseComponent();
		WebSite webSite = (WebSite) component.getBean("webSitesHib");
		sitePageEditForm.setWebSites(webSite.findAll());
		sitePageEditForm.setIdWebSite(sitePage.getWebSite().getId());

		Sheet sheet = (Sheet) component.getBean("sheetsHib");
		sitePageEditForm.setSheets(sheet.findAll());
		sitePageEditForm.setIdSheet(sitePage.getSheet().getId());

		return mapping.findForward("showEdit");
	}

	public ActionForward deleteSitePage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SitePageEditForm sitePageEditForm = (SitePageEditForm) form;

		Long id = new Long(Long.parseLong(request.getParameter("id")));

		sitePageEditForm.getSitePage().setId(id);
		sitePageEditForm.getSitePage().delete();

		return mapping.findForward("showList");
	}

	public ActionForward addSitePage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		SitePageEditForm sitePageEditForm = (SitePageEditForm) form;

		Component component = new DataBaseComponent();
		WebSite webSite = (WebSite) component.getBean("webSitesHib");
		sitePageEditForm.setWebSites(webSite.findAll());

		Sheet sheet = (Sheet) component.getBean("sheetsHib");
		sitePageEditForm.setSheets(sheet.findAll());

		return mapping.findForward("showAdd");
	}

	public ActionForward saveSitePage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SitePageEditForm sitePageEditForm = (SitePageEditForm) form;

		Component component = new DataBaseComponent();
		WebSite webSite = (WebSite) component.getBean("webSitesHib");

		webSite.setId(sitePageEditForm.getIdWebSite());

		try {
			webSite.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		Sheet sheet = (Sheet) component.getBean("sheetsHib");

		sheet.setId(sitePageEditForm.getIdSheet());

		try {
			sheet.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		SitePage sitePage = sitePageEditForm.getSitePage();
		sitePage.setWebSite(webSite);
		sitePage.setSheet(sheet);
		sitePage.save();

		return mapping.findForward("showList");

	}

	public ActionForward updateSitePage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SitePageEditForm sitePageEditForm = (SitePageEditForm) form;

		Component component = new DataBaseComponent();
		WebSite webSite = (WebSite) component.getBean("webSitesHib");
		webSite.setId(sitePageEditForm.getIdWebSite());

		try {
			webSite.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		Sheet sheet = (Sheet) component.getBean("sheetsHib");
		sheet.setId(sitePageEditForm.getIdSheet());

		try {
			sheet.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		SitePage sitePage = sitePageEditForm.getSitePage();
		sitePage.setWebSite(webSite);
		sitePage.setSheet(sheet);
		sitePage.update();

		return mapping.findForward("showList");

	}

}